<?php

namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;

class Lostuser extends Model
{
    protected $connection = 'mysql_thor';

    protected $table = 'users';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
       // 'name', 'email', 'mobile',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
    ];

//
//    public function channel()
//    {
//        return $this->belongsTo(Channel::class);
//    }
//
//    public function auditor()
//    {
//        return $this->belongsTo(AdminUser::class,'auditor_id');
//    }
//
//    public function order(){
//        return $this->hasMany(Order::class, 'user_id');
//    }


    public function paginate()
    {
        $perPage = Request::get('per_page', 20);

        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;

        // 运行sql获取数据数组
        $sql = 'select u.id uid,u.verify_name uverify_name,u.mobile umobile,c.name cname,u.idcard_ok,u.alive_ok,u.carrier_ok,u.taobaoweb_ok,u.bankcard_ok,u.created_at ucreated_at,u.updated_at uupdated_at,o.state ostate from users u LEFT JOIN channels c on u.channel_id = c.id RIGHT JOIN orders o on u.id = o.user_id GROUP BY u.id having o.state limit '.$start. ",".$perPage;

        $result = DB::select($sql);



        $total = 100;
        $movies = static::hydrate($result);

        $paginator = new LengthAwarePaginator($movies, $total, $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    public static function with($relations)
    {
        return new static;
    }
}
